﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Media;

namespace PhotoViewer
{
    public partial class frmSlideshow : Form
    {

        SoundPlayer soundPlayer = new SoundPlayer();
        public frmSlideshow()
        {
            InitializeComponent();
            Random random = new Random();
            int num=random.Next(5);
            switch (num.ToString())
            { 
                case "0":
                    soundPlayer.SoundLocation = @"..\..\Music\Falling Slowly.wav";
                    soundPlayer.Play();
                    break;

                case "1":
                    soundPlayer.SoundLocation = @"..\..\Music\kiss the rain.wav";
                    soundPlayer.Play();
                    break;
                case "2":
                    soundPlayer.SoundLocation = @"..\..\Music\爱情雨.wav";
                    soundPlayer.Play();
                    break;
                case "3":
                    soundPlayer.SoundLocation = @"..\..\Music\秋日私语.wav";
                    soundPlayer.Play();
                    break;
                case "4":
                    soundPlayer.SoundLocation = @"..\..\Music\卡农.wav";
                    soundPlayer.Play();
                    break;
            }
        }
        public string picDiretoryPath;//用户打开的图片文件夹目录
        FileSystemInfo[] fileSysInfo;
        DirectoryInfo dirInfo;
        int i = 0;
        public string filePath;

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmSlideshow_Load(object sender, EventArgs e)
        {
            //设置窗体的text值
            this.Text = picDiretoryPath;
            dirInfo = new DirectoryInfo(picDiretoryPath);
            fileSysInfo = dirInfo.GetFileSystemInfos();//获得该目录下的所有图片+后缀名

            if (picDiretoryPath.Length <= 4)
            {
                filePath = picDiretoryPath;
            }
            else
            {
                filePath = picDiretoryPath + "\\";
            }
        }

        /// <summary>
        ///timer 在interval间隔内调用getPic（）;
        /// </summary>
        private void GetPic()
        {
            if (i < fileSysInfo.Length)
            {
                //获取文件后缀名
                string imageType= fileSysInfo[i].ToString().Substring(fileSysInfo[i].ToString().LastIndexOf(".") + 1, (fileSysInfo[i].ToString().Length - fileSysInfo[i].ToString().LastIndexOf(".") - 1));
                imageType = imageType.ToLower();

                if (imageType== "jpg" || imageType== "png" || imageType== "bmp" || imageType== "gif" || imageType== "jpeg")
                {
                    //filePath文件路径
                    //fileSysInfo[i]图片名和后缀名
                    pictureBox1.Image = Image.FromFile(filePath + fileSysInfo[i].ToString());
                }
            }
        }

       

        /// <summary>
        /// 重新播放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnReplay_Click(object sender, EventArgs e)
        {
            i = 0;//从头开始播放
            timer1.Start();//相当于timer.Enablee=true;开始计时
            tsbtnStop.Text = "Stop";//将stop按钮设置成为暂停
        }

        /// <summary>
        /// Stop/Keep on
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnStop_Click(object sender, EventArgs e)
        {
            if (tsbtnStop.Text == "Stop")
            {
                tsbtnStop.Text = "Continue";
                timer1.Stop();//停止获取图片
            }
            else
            {
                tsbtnStop.Text = "Stop";
                timer1.Start();//重新开始获取图片
            }
        }

        /// <summary>
        /// 在Interval间隔内会执行Timer Tick事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            GetPic();
            i++;
        }

        private void tsBtnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to close the window?", "Prompt", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                this.Close();
                soundPlayer.Stop();//停止音乐播放
                
               
            }
        }

    }
}
